package com.lcifn.middleware.rpc.hello;

import com.lcifn.middleware.rpc.NameService;
import com.lcifn.middleware.rpc.RpcAccessPoint;
import com.lcifn.middleware.rpc.spi.ServiceSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URI;

/**
 * Hello服务客户端
 * @author lichao
 * @date 2021/2/1 13:54
 */
public class HelloRpcClient {

    private static final Logger logger = LoggerFactory.getLogger(HelloRpcClient.class);

    public static void main(String[] args) {
        RpcAccessPoint rap = ServiceSupport.load(RpcAccessPoint.class);
        // 1.获取Name服务
        String redisUri = "redis://192.168.2.18:6379";
        URI redisNameServiceUri = URI.create(redisUri);
        NameService nameService = rap.getNameService(redisNameServiceUri);
        assert nameService != null;
        // 2.从Name服务获取HelloService服务提供者列表
        String helloServiceName = HelloService.class.getCanonicalName();
        URI helloServiceProviderUri = nameService.lookupService(helloServiceName);
        assert helloServiceProviderUri != null;
        logger.info("get service {} provider uri: {} ", helloServiceName, helloServiceProviderUri);
        // 3.连接服务提供者，创建HelloService桩服务
        HelloService helloServiceStub = rap.getRemoteService(helloServiceProviderUri, HelloService.class);
        // 4.调用桩服务方法，执行远程访问
        String requestName = "request rpc server";
        logger.info("client request：{} ", requestName);
        String serverResp = helloServiceStub.hello(requestName);
        logger.info("server response: {} ", serverResp);
    }

}
